
package com.tonyodev.fetch2;

import com.tonyodev.fetch2core.FetchErrorStrings;

import java.io.IOException;
import java.net.SocketTimeoutException;

/**
 * FetchErrorUtils
 *
 * @since 2021-05-20
 */
public class FetchErrorUtils {
    private FetchErrorUtils() {
    }

    /**
     * getErrorFromThrowable
     *
     * @param throwable throwable
     * @return Error
     */
    public static Error getErrorFromThrowable(Throwable throwable) {
        String message = throwable.getMessage();
        if (message == null || message.equals("")) {
            message = "";
        }
        if (throwable instanceof SocketTimeoutException && message.isEmpty()) {
            message = FetchErrorStrings.CONNECTION_TIMEOUT;
        }
        Error error = getErrorFromMessage(message);
        if (error == Error.UNKNOWN && throwable instanceof SocketTimeoutException) {
            error = Error.CONNECTION_TIMED_OUT;
        } else if (error == Error.UNKNOWN && throwable instanceof IOException) {
            error = Error.UNKNOWN_IO_ERROR;
        }
        error.setThrowable(throwable);
        return error;
    }

    /**
     * getErrorFromMessage
     *
     * @param message message
     * @return Error
     */
    public static Error getErrorFromMessage(String message) {
        if (message == null || message.isEmpty()) {
            return Error.UNKNOWN;
        } else if (message.equalsIgnoreCase(FetchErrorStrings.REQUEST_WITH_FILE_PATH_ALREADY_EXIST)
            || message.contains(FetchErrorStrings.FAILED_TO_ENQUEUE_REQUEST_FILE_FOUND)) {
            return Error.REQUEST_WITH_FILE_PATH_ALREADY_EXIST;
        } else if (message.contains(FetchErrorStrings.UNIQUE_ID_DATABASE)) {
            return Error.REQUEST_WITH_ID_ALREADY_EXIST;
        } else if (message.contains(FetchErrorStrings.EMPTY_RESPONSE_BODY)) {
            return Error.EMPTY_RESPONSE_FROM_SERVER;
        } else if (message.equalsIgnoreCase(FetchErrorStrings.FNC) || message.equalsIgnoreCase(FetchErrorStrings.ENOENT)) {
            return Error.FILE_NOT_CREATED;
        } else if (message.contains(FetchErrorStrings.ETIMEDOUT)
            || message.contains(FetchErrorStrings.CONNECTION_TIMEOUT)
            || message.contains(FetchErrorStrings.SOFTWARE_ABORT_CONNECTION)
            || message.contains(FetchErrorStrings.READ_TIME_OUT)) {
            return Error.CONNECTION_TIMED_OUT;
        } else if (message.equalsIgnoreCase(FetchErrorStrings.IO404) || message.contains(FetchErrorStrings.NO_ADDRESS_HOSTNAME)) {
            return Error.HTTP_NOT_FOUND;
        } else if (message.contains(FetchErrorStrings.HOST_RESOLVE_ISSUE)) {
            return Error.UNKNOWN_HOST;
        } else if (message.equalsIgnoreCase(FetchErrorStrings.EACCES)) {
            return Error.WRITE_PERMISSION_DENIED;
        } else if (message.equalsIgnoreCase(FetchErrorStrings.ENOSPC)
            || message.equalsIgnoreCase(FetchErrorStrings.DATABASE_DISK_FULL)) {
            return Error.NO_STORAGE_SPACE;
        } else if (message.equalsIgnoreCase(FetchErrorStrings.FAILED_TO_ENQUEUE_REQUEST)) {
            return Error.REQUEST_ALREADY_EXIST;
        } else if (message.equalsIgnoreCase(FetchErrorStrings.DOWNLOAD_NOT_FOUND)) {
            return Error.DOWNLOAD_NOT_FOUND;
        } else if (message.equalsIgnoreCase(FetchErrorStrings.FETCH_DATABASE_ERROR)) {
            return Error.FETCH_DATABASE_ERROR;
        } else if (message.contains(FetchErrorStrings.RESPONSE_NOT_SUCCESSFUL) || message.contains(FetchErrorStrings.FAILED_TO_CONNECT)) {
            return Error.REQUEST_NOT_SUCCESSFUL;
        } else if (message.contains(FetchErrorStrings.INVALID_CONTENT_HASH)) {
            return Error.INVALID_CONTENT_HASH;
        } else if (message.contains(FetchErrorStrings.DOWNLOAD_INCOMPLETE)) {
            return Error.UNKNOWN_IO_ERROR;
        } else if (message.contains(FetchErrorStrings.FAILED_TO_UPDATE_REQUEST)) {
            return Error.FAILED_TO_UPDATE_REQUEST;
        } else if (message.contains(FetchErrorStrings.FAILED_TO_ADD_COMPLETED_DOWNLOAD)) {
            return Error.FAILED_TO_ADD_COMPLETED_DOWNLOAD;
        } else if (message.contains(FetchErrorStrings.FETCH_FILE_SERVER_INVALID_RESPONSE_TYPE)) {
            return Error.FETCH_FILE_SERVER_INVALID_RESPONSE;
        } else if (message.contains(FetchErrorStrings.REQUEST_DOES_NOT_EXIST)) {
            return Error.REQUEST_DOES_NOT_EXIST;
        } else if (message.contains(FetchErrorStrings.NO_NETWORK_CONNECTION)) {
            return Error.NO_NETWORK_CONNECTION;
        } else if (message.contains(FetchErrorStrings.FILE_NOT_FOUND)) {
            return Error.FILE_NOT_FOUND;
        } else if (message.contains(FetchErrorStrings.FETCH_FILE_SERVER_URL_INVALID)) {
            return Error.FETCH_FILE_SERVER_URL_INVALID;
        } else if (message.contains(FetchErrorStrings.ENQUEUED_REQUESTS_ARE_NOT_DISTINCT)) {
            return Error.ENQUEUED_REQUESTS_ARE_NOT_DISTINCT;
        } else if (message.contains(FetchErrorStrings.ENQUEUE_NOT_SUCCESSFUL)) {
            return Error.ENQUEUE_NOT_SUCCESSFUL;
        } else if (message.contains(FetchErrorStrings.FAILED_RENAME_FILE_ASSOCIATED_WITH_INCOMPLETE_DOWNLOAD)) {
            return Error.FAILED_TO_RENAME_INCOMPLETE_DOWNLOAD_FILE;
        } else if (message.contains(FetchErrorStrings.FILE_CANNOT_BE_RENAMED)) {
            return Error.FAILED_TO_RENAME_FILE;
        } else if (message.contains(FetchErrorStrings.FILE_ALLOCATION_ERROR)) {
            return Error.FILE_ALLOCATION_FAILED;
        } else if (message.contains(FetchErrorStrings.CLEAR_TEXT_NETWORK_VIOLATION)) {
            return Error.HTTP_CONNECTION_NOT_ALLOWED;
        } else {
            return Error.UNKNOWN;
        }
    }
}
